
const config = require('../config/config.js')
const db     = require('monk')( `${config.mongodb.host}:${config.mongodb.port}/${config.mongodb.dbname}` );

class mongodb {
    dbState(){
        return db._state;
    }
    findAll(table, condition){
        let tbl = db.get(table)
        return tbl.find(condition)
    }
    findLimit(table, condition, page=1, pagesize=20){
        let tbl = db.get(table)
        return tbl.find( condition, {"limit":pagesize,"skip":(page-1)*pagesize} )
    }
    findLimitCount(table, condition){
        let tbl = db.get(table)
        return tbl.count( condition )
    }

    insert(table, obj){
        let tbl = db.get(table)
        return tbl.insert(obj)
    }
    update(table, obj, condition, insert_new=false ){
        let tbl = db.get(table)
        return tbl.update(condition, obj, insert_new)
    }
    remove(table, condition){
        let tbl = db.get(table)
        return tbl.remove(condition)
    }
}
module.exports = new mongodb()